package com.board.controller;


import cn.hutool.core.bean.BeanUtil;
import com.board.dto.ResourceDto;
import com.board.entity.Resource;
import com.board.form.ResourceAddForm;
import com.board.param.ResourceAddParam;
import com.board.service.ResourceService;
import com.board.util.MD5Util;
import com.board.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源信息接口")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 查询所有资源
     * @return 返回所有资源
     */
    @GetMapping("/findAllResource")
    @ApiOperation("查询所有资源")
    public Result findAllResource(){
        List<ResourceDto> list=resourceService.findAllResource();
        return Result.success("查询所有资源成功",list);
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.md5("admin", "123456"));
    }

    /**
     * 新增资源
     * @param form 新增资源信息，包含资源名称，资源url，父资源id，资源等级
     * @return 返回新增的资源
     */
    @PostMapping("/addResource")
    @ApiOperation("新增资源")
    public Result addResource(@RequestBody ResourceAddForm form){
        Resource resource = resourceService.addResource(BeanUtil.toBean(form, ResourceAddParam.class));
        return Result.success("新增资源成功",resource);
    }
}

